
import Vue from 'vue'
import axios from 'axios'
import vuex from './store'
const http = axios.create(
  {
    baseURL: '/',
    timeout: 10000
  }
)
http.interceptors.request.use(function (config) {
  return config
}, function (error) {
  return Promise.reject(error)
})
http.interceptors.response.use(res => {
  return res
}, err => {
  const { message } = err
  if (message.includes('timeout')) {
    Vue.prototype.$notify({
      message: '请求超时，请重新发起请求',
      duration: 2000
    })
  }
  if (err.response.status === 423) {
    Vue.prototype.$toast.fail(err.response.data.message)
    vuex.commit('removeUser')
  } else if (err.response.status === 422) {
    Vue.prototype.$toast.fail(err.response.data.message)
  } else if (err.response.status === 500 || err.response.status === 404) {
    Vue.prototype.$notify({
      message: '系统接口异常,请联系管理员',
      duration: 2000
    })
  }

  return Promise.reject(err)
})
export default http
